﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;

namespace LibraryManagement
{
    public class Helper
    {
        
        /// <summary>
        /// Binds the list.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="enumObject">The enum object.</param>
        /// <remarks></remarks>
        public static void BindList(ListControl list, Type enumObject)
        {
            list.DataValueField = "value";
            list.DataTextField = "text";
            list.DataSource = GetDataTableFromEnum(enumObject, "value", "text");
            try
            {
                object[] attributes = enumObject.GetCustomAttributes(false);
                if (attributes.Length > 0)
                    list.SelectedValue = ((EnumAttribute)attributes[0]).DefaultValue.ToString();
                list.DataBind();
            }
            catch { }
        }


        /// <summary>
        /// Dates the time to short date string.
        /// </summary>
        /// <param name="textboxValue">The textbox value.</param>
        /// <returns></returns>
        public static Nullable<DateTime> DateTimeToShortDateString(String textboxValue)
        {
            if (textboxValue == String.Empty)
            {
                return null;
            }
            return Convert.ToDateTime(textboxValue);
        }


        /// <summary>
        /// Converts to integer.
        /// </summary>
        /// <param name="textboxValue">The textbox value.</param>
        /// <returns></returns>
        public static Nullable<Int32> ConvertToInteger(String textboxValue)
        {
            if (textboxValue == String.Empty)
            {
                return null;
            }
            return Convert.ToInt32(textboxValue);
        }
        /// <summary>
        /// Converts to short integer.
        /// </summary>
        /// <param name="textboxValue">The textbox value.</param>
        /// <returns></returns>
        public static Nullable<Int16> ConvertToShortInteger(String textboxValue)
        {
            if (textboxValue == String.Empty)
            {
                return null;
            }
            return Convert.ToInt16(textboxValue);
        }
        /// <summary>
        /// Converts to decimal.
        /// </summary>
        /// <param name="textboxValue">The textbox value.</param>
        /// <returns></returns>
        public static Nullable<Decimal> ConvertToDecimal(String textboxValue)
        {

            if (textboxValue == String.Empty)
            {
                return null;
            }
            return Convert.ToDecimal(textboxValue);
        }

        /// <summary>
        /// Gets the data table from enum.
        /// </summary>
        /// <param name="enumObject">The enum object.</param>
        /// <param name="valueColumn">The value column.</param>
        /// <param name="textColumn">The text column.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static DataTable GetDataTableFromEnum(Type enumObject, string valueColumn, string textColumn)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(valueColumn);
            dataTable.Columns.Add(textColumn);
            foreach (object enumItem in Enum.GetValues(enumObject))
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow[0] = (Int32)(enumItem);
                dataRow[1] = enumItem.ToString();
                dataTable.Rows.Add(dataRow);
            }
            return dataTable;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    [AttributeUsage(AttributeTargets.Enum)]
    public sealed class EnumAttribute : Attribute
    {
        /// <summary>
        /// class constructor
        /// </summary>
        /// <remarks></remarks>
        public EnumAttribute()
        {
        }
        /// <summary>
        /// Gets or Sets Default value to display in list control
        /// used to set default value in list if the list filed by an enum
        /// </summary>
        public Int32 DefaultValue
        {
            get { return defaultValueInList; }
            set { defaultValueInList = value; }
        }private Int32 defaultValueInList = Int32.MinValue;
    }

    public class PlainHtmlHelper
    {
        //Members

        private const String constTDStart = "#!$CELL_START$!#";
        private const String constTDEnd = "#!$CELL_END$!#";
        private const String constTRStart = "#!$ROW_START$!#";
        private const String constTREnd = "#!$ROW_END$!#";
        private const String constTableStart = "#!$TABLE_START$!#";
        private const String constTableEnd = "#!$TABLE_END$!#";



//Calling

            //String plainHTML = StripHTML(Description);
            //plainHTML = ProcessTable(plainHTML);



  /// <summary>
        /// Strips the HTML.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static String StripHTML(String source)
        {
            try
            {
                String result;
                // Remove HTML Development formatting
                // Replace line breaks with space
                // because browsers inserts space
                result = source.Replace("\r", " ");
                // Replace line breaks with space
                // because browsers inserts space
                result = result.Replace("\n", " ");
                // Remove step-formatting
                result = result.Replace("\t", string.Empty);
                // Remove repeating spaces because browsers ignore them
                result = System.Text.RegularExpressions.Regex.Replace(result,
                                                                      @"( )+", " ");

                // Remove the header (prepare first by clearing attributes)
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<( )*head([^>])*>", "<head>",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"(<( )*(/)( )*head( )*>)", "</head>",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(<head>).*(</head>)", String.Empty,
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // remove all scripts (prepare first by clearing attributes)
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<( )*script([^>])*>", "<script>",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"(<( )*(/)( )*script( )*>)", "</script>",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"(<script>).*(</script>)", String.Empty,
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // remove all styles (prepare first by clearing attributes)
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<( )*style([^>])*>", "<style>",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"(<( )*(/)( )*style( )*>)", "</style>",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(<style>).*(</style>)", String.Empty,
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // insert our constants in spaces of </td> and <td> tags
                result = System.Text.RegularExpressions.Regex.Replace(result,
                        @"</( )*td([^>])*>", " " + constTDEnd + " ",
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                result = System.Text.RegularExpressions.Regex.Replace(result,
                            @"<( )*td([^>])*>", " " + constTDStart + " ",
                            System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // insert line breaks in places of <BR> and <LI> tags
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<( )*br( )*/>", "\r",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<( )*li( )*>", "\r",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                //insert our constants in spaces of </tr> and <tr> tags
                result = System.Text.RegularExpressions.Regex.Replace(result,
                        @"</( )*tr([^>])*>", " " + constTREnd + " ",
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                result = System.Text.RegularExpressions.Regex.Replace(result,
                             @"<( )*tr([^>])*>", " " + constTRStart + " ",
                             System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                //insert our constants in spaces of </tr> and <tr> tags
                result = System.Text.RegularExpressions.Regex.Replace(result,
                        @"</( )*table([^>])*>", " " + constTableEnd + " ",
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                result = System.Text.RegularExpressions.Regex.Replace(result,
                             @"<( )*table([^>])*>", " " + constTableStart + " ",
                             System.Text.RegularExpressions.RegexOptions.IgnoreCase);


                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<( )*p([^>])*>", "\r",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // Remove remaining tags like <a>, links, images,
                // comments etc - anything that's enclosed inside < >
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"<[^>]*>", String.Empty,
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // replace special characters:
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @" ", " ",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&bull;", " * ",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&lsaquo;", "<",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&rsaquo;", ">",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&trade;", "(tm)",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&frasl;", "/",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&lt;", "<",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&gt;", ">",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&copy;", "(c)",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         @"&reg;", "(r)",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                // make line breaking consistent
                result = result.Replace("\n", "\r");

                // Remove extra line breaks and tabs:
                // replace over 2 breaks with 2 and over 4 tabs with 4.
                // Prepare first to remove any whitespaces in between
                // the escaped characters and remove redundant tabs in between line breaks
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(\r)( )+(\r)", "\r\r",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(\t)( )+(\t)", "\t\t",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(\t)( )+(\r)", "\t\r",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(\r)( )+(\t)", "\r\t",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                // Remove redundant tabs
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(\r)(\t)+(\r)", "\r\r",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                // Remove multiple tabs following a line break with just one tab
                result = System.Text.RegularExpressions.Regex.Replace(result,
                         "(\r)(\t)+", "\r\t",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                // Initial replacement target string for line breaks
                string breaks = "\r\r\r";
                // Initial replacement target string for tabs
                string tabs = "\t\t\t\t\t";
                for (int index = 0; index < result.Length; index++)
                {
                    result = result.Replace(breaks, "\r\r");
                    result = result.Replace(tabs, "\t\t\t\t");
                    breaks = breaks + "\r";
                    tabs = tabs + "\t";
                }

                result = result.Replace("\r", "<br/>");
                result = ProcessFormattedHTML(result);
                return result;
            }
            catch
            {
                //MessageBox.Show("Error");
                return source;
            }
        }


        /// <summary>
        /// Processes the formatted HTML.
        /// </summary>
        /// <param name="html">The HTML.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static String ProcessFormattedHTML(String html)
        {
            Int32 tblStartIndex = html.IndexOf(constTableStart);
            Int32 tblEndIndex = html.IndexOf(constTableEnd);
            String formattedString = String.Empty;

            if (tblStartIndex >= 0 && tblEndIndex >= 0)
            {
                while (tblStartIndex >= 0)
                {
                    String partOne = html.Substring(0, html.IndexOf(constTableStart) - 1);
                    String table = html.Substring(tblStartIndex, html.IndexOf(constTableEnd, tblStartIndex) - html.IndexOf(constTableStart, tblStartIndex) + constTableEnd.Length);
                    String partTwo = html.Substring(html.IndexOf(constTableEnd) + constTableEnd.Length);
                    formattedString = ProcessTable(table);

                    html = partOne + formattedString + partTwo;

                    tblStartIndex = html.IndexOf(constTableStart);
                }
            }
            return html;
        }

        /// <summary>
        /// Processes the table.
        /// </summary>
        /// <param name="html">The HTML.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static String ProcessTable(String html)
        {
            Int32 tblStartIndex = html.IndexOf(constTRStart);
            Int32 tblEndIndex = html.LastIndexOf(constTREnd);

            if (tblStartIndex >= 0 && tblEndIndex >= 0)
            {
                Int32 len = constTREnd.Length;
                String resultTable = String.Empty;
                String partOne = html.Substring(0, tblStartIndex - 1);
                String partTable = html.Substring(tblStartIndex, tblEndIndex - tblStartIndex + len);
                String partTwo = html.Substring(tblEndIndex + len);

                ArrayList TdInRows = new ArrayList();
                Int32 pos = partTable.IndexOf(constTRStart);
                while (pos >= 0)
                {
                    String Row = partTable.Substring(pos, partTable.IndexOf(constTREnd, pos) - partTable.IndexOf(constTRStart, pos) + constTREnd.Length);
                    pos = partTable.IndexOf(constTRStart, partTable.IndexOf(constTREnd, pos));
                    TdInRows.Add(ProcessRow(Row));
                }

                //If rows are two then only we are processing
                if (TdInRows.Count == 2)
                {
                    ArrayList firstArray = TdInRows[0] as ArrayList;
                    ArrayList secondArray = TdInRows[1] as ArrayList;

                    if (firstArray.Count == secondArray.Count)
                    {
                        for (int i = 0; i < secondArray.Count; i++)
                        {
                            resultTable = resultTable + secondArray[i].ToString() + ":" + firstArray[i].ToString() + ",";
                        }
                    }
                }
                else if (TdInRows.Count == 3)
                {
                    ArrayList firstArray = TdInRows[0] as ArrayList;
                    ArrayList secondArray = TdInRows[1] as ArrayList;
                    ArrayList thirdArray = TdInRows[2] as ArrayList;

                    if (firstArray.Count == secondArray.Count)
                    {
                        for (int i = 0; i < secondArray.Count; i++)
                        {
                            resultTable = resultTable + secondArray[i].ToString() + ":" + firstArray[i].ToString() + ":" + thirdArray[i].ToString() + ",";
                        }
                    }
                }


                if (resultTable != String.Empty && resultTable.LastIndexOf(",") > 0)
                    resultTable = resultTable.Remove(resultTable.LastIndexOf(","));

                return resultTable;
            }
            else
            {
                return html;
            }
        }

        /// <summary>
        /// Processes the row.
        /// </summary>
        /// <param name="tblRow">The TBL row.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static ArrayList ProcessRow(String tblRow)
        {
            ArrayList arrTDs = new ArrayList();

            Int32 pos = tblRow.IndexOf(constTDStart);
            while (pos >= 0)
            {
                String Cell = tblRow.Substring(pos, tblRow.IndexOf(constTDEnd, pos) - tblRow.IndexOf(constTDStart, pos) + constTDEnd.Length);
                pos = tblRow.IndexOf(constTDStart, tblRow.IndexOf(constTDEnd, pos));
                arrTDs.Add(GetCellValue(Cell));
            }
            return arrTDs;
        }

        /// <summary>
        /// Gets the cell value.
        /// </summary>
        /// <param name="Cell">The cell.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static String GetCellValue(String Cell)
        {
            String value = String.Empty;
            Int32 cellStart = Cell.IndexOf(constTDStart) + constTDStart.Length;
            Int32 cellEnd = Cell.IndexOf(constTDEnd) - cellStart;
            value = Cell.Substring(cellStart, cellEnd);
            return value;
        }



    }
}
